/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DB;

import Model.Status;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Tomas
 */
public class StatusManager implements IFStatusManager{

    private Connection con;

    public StatusManager(){
        con = DBConnector.getInstance().getDBConnection();
    }

    public ArrayList<Status> getStatuses(String type) throws IllegalArgumentException {
       return multipleWhere("type = ?",new String[]{type});
    }
    private ArrayList<Status> multipleWhere(String whereC, String[] params){
        ResultSet results;
        String query = buildQuery(whereC);
        ArrayList<Status> resultItem = new ArrayList<Status>();
        try{
            PreparedStatement stmt = con.prepareStatement(query);
            for(int a = 0; a < params.length; a++){
              stmt.setString(a+1, params[a]);
            }
            stmt.setQueryTimeout(5);
            results = stmt.executeQuery();
            while(results.next()){
                resultItem.add(buildStatus(results));
            }
            stmt.close();

        }catch(Exception e){
            System.out.println(e.getMessage());
        }
        if(resultItem.isEmpty()){
            throw new IllegalArgumentException("Status not found");
        }
        return resultItem;
    }
    private Status singleWhere(String whereC, String[] params){
        ResultSet results;
        String query = buildQuery(whereC);
        Status resultItem = null;
        try{
            PreparedStatement stmt = con.prepareStatement(query);
            for(int a = 0; a < params.length; a++){
              stmt.setString(a+1, params[a]);
            }
            stmt.setQueryTimeout(5);
            results = stmt.executeQuery();
            if(results.next()){
                resultItem = buildStatus(results);
            }
            stmt.close();

        }catch(Exception e){
            e.printStackTrace();
        }
        if(resultItem == null){
            throw new IllegalArgumentException("Status not found");
        }
        return resultItem;
    }
    private void multipleWhere(){
        
    }

     private String buildQuery(String whereC){
        String query = "SELECT * FROM Status";
        if(!whereC.isEmpty()){
            query = query+" WHERE "+whereC;
        }
        return query;
    }
    private Status buildStatus(ResultSet resSet){
        Status item = null;
        try {
            item = new Status(resSet.getInt(1),resSet.getString(2),
                    resSet.getString(3));
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return item;
    }

    public Status getStatusByID(int statusId) throws IllegalArgumentException {
        String param = ""+statusId;
        return singleWhere("statusId = ?", new String[]{param});
    }

    public Status getStatusByName(String name) throws IllegalArgumentException {
        return singleWhere("name = ?", new String[]{name});
    }
}
